# delimit ;

clear;
*version 13;
set matsize 400;
set more off;


log using "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexTheoretical/Conditional/Severity/PredictedValues.log", replace;
cd "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexTheoretical/Conditional/Severity";

**************************************************************;
*Author: Jeff Carter                                         *;
*Date: Saturday, May 23, 2015                              *;
*Purpose: Simulating Predictions                *;
**************************************************************;

** Need Legend for Probabilities and Differences  **;

use "/Users/Jeff/Dropbox/TermLimits/Replication/Models/IndexTheoretical/Conditional/Severity/SimData.dta";

postutil clear;

postfile mypost prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 prob_hat2 lo2 hi2 prob_hat3 lo3 hi3 prob_hat4 lo4 hi4 prob_hat5 lo5 hi5
                diff_hat0 diff_lo0 diff_hi0 diff_hat1 diff_lo1 diff_hi1 diff_hat2 diff_lo2 diff_hi2 
                prob_hat01 lo01 hi01 prob_hat11 lo11 hi11 prob_hat21 lo21 hi21 prob_hat31 lo31 hi31  prob_hat41 lo41 hi41 prob_hat51 lo51 hi51
                diff_hat01 diff_lo01 diff_hi01 diff_hat11 diff_lo11 diff_hi11 diff_hat21 diff_lo21 diff_hi21
                delta_hat delta_lo delta_hi delta_hat1 delta_lo1 delta_hi1 delta_hat2 delta_lo2 delta_hi2
                using PredictedValues, replace;

noisily display "start";


local a=0 ;
while `a' < 4 { ;

{;



scalar h_Accountable =   0  ;
scalar h_LameDuck= 1  ;
scalar h_Civilian = 0;
scalar h_Military = 1;
scalar h_Constant = 1;
scalar h_Parliament = 0;
scalar h_CivilWar = 0;
scalar h_GDPpc =  12589.35;
scalar h_Growth = 3.453132;
scalar h_MajorPower = 0;
scalar h_Borders = 2;
scalar h_Mobilization =  -.0008714 ;
scalar h_Time = 5; 
scalar h_Rivalry = 0;
scalar h_Female = 0;


generate x_betahat0 = MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);

generate x_betahat1 = MG_b1* h_LameDuck
    + MG_b2*`a'
    + MG_b3*(h_LameDuck*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);


gen prob0=  1/(1+exp(x_betahat0-MG_b11));
gen prob1=   (1/(1+exp(x_betahat0-MG_b12)))  -  (1/(1+exp(x_betahat0-MG_b11)))  ;
gen prob2=   1- (1/(1+exp(x_betahat0-MG_b12)));

gen prob3=  1/(1+exp(x_betahat1-MG_b11));
gen prob4=  (1/(1+exp(x_betahat1-MG_b12)))  -  (1/(1+exp(x_betahat1-MG_b11)))  ;
gen prob5=   1- (1/(1+exp(x_betahat1-MG_b12)));

        
gen diff0=prob3-prob0 ;
gen diff1=prob4-prob1 ;
gen diff2=prob5-prob2 ;



        
egen probhat0=mean(prob0) ;
egen probhat1=mean(prob1) ;
egen probhat2=mean(prob2) ;
egen probhat3=mean(prob3) ;
egen probhat4=mean(prob4) ;
egen probhat5=mean(prob5) ;


egen diffhat0=mean(diff0) ;
egen diffhat1=mean(diff1) ;
egen diffhat2=mean(diff2) ;





 
generate x_betahat01 =   MG_b1*h_Accountable
    + MG_b2*`a'
    + MG_b3*(h_Accountable*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);
 
generate x_betahat11 =   MG_b1* h_LameDuck
    + MG_b2*`a'
    + MG_b3*(h_LameDuck*`a')
    + MG_b4*h_Rivalry
    + MG_b5*h_Borders
    + MG_b6*h_Parliament
    + MG_b7*h_Female 
    + MG_b8*h_Time 
    + MG_b9*(h_Time^2)
    + MG_b10*(h_Time^3);



gen prob01=  1/(1+exp(x_betahat01-MG_b11));
gen prob11=   (1/(1+exp(x_betahat01-MG_b12)))  -  (1/(1+exp(x_betahat01-MG_b11)))  ;
gen prob21=   1- (1/(1+exp(x_betahat01-MG_b12)));

gen prob31=  1/(1+exp(x_betahat11-MG_b11));
gen prob41=  (1/(1+exp(x_betahat11-MG_b12)))  -  (1/(1+exp(x_betahat11-MG_b11)))  ;
gen prob51=   1- (1/(1+exp(x_betahat11-MG_b12)));
        
gen diff01=prob31-prob01 ;
gen diff11=prob41-prob11 ;
gen diff21=prob51-prob21 ;



        
egen probhat01=mean(prob01) ;
egen probhat11=mean(prob11) ;
egen probhat21=mean(prob21) ;
egen probhat31=mean(prob31) ;
egen probhat41=mean(prob41) ;
egen probhat51=mean(prob51) ;

egen diffhat01=mean(diff01) ;
egen diffhat11=mean(diff11) ;
egen diffhat21=mean(diff21) ;



        
gen delta= diff01-diff0 ;
gen delta1 = diff11-diff1 ;
gen delta2 = diff21-diff2 ;
egen deltahat=mean(delta) ;
egen deltahat1=mean(delta1) ;
egen deltahat2=mean(delta2) ;




tempname prob_hat0 lo0 hi0 prob_hat1 lo1 hi1 prob_hat2 lo2 hi2 prob_hat3 lo3 hi3 prob_hat4 lo4 hi4 prob_hat5 lo5 hi5
        diff_hat0 diff_lo0 diff_hi0 diff_hat1 diff_lo1 diff_hi1 diff_hat2 diff_lo2 diff_hi2
        prob_hat01 lo01 hi01 prob_hat11 lo11 hi11 prob_hat21 lo21 hi21 prob_hat31 lo31 hi31 prob_hat41 lo41 hi41 prob_hat51 lo51 hi51
        diff_hat01 diff_lo01 diff_hi01 diff_hat11 diff_lo11 diff_hi11 diff_hat21 diff_lo21 diff_hi21
        delta_hat delta_lo delta_hi delta_hat1 delta_lo1 delta_hi1 delta_hat2 delta_lo2 delta_hi2;




        
    _pctile prob0, p(2.5,97.5) ;
    scalar `lo0'=r(r1) ;
    scalar `hi0'=r(r2) ;

    _pctile prob1, p(2.5,97.5) ;
    scalar `lo1'=r(r1) ;
    scalar `hi1'=r(r2) ;

    _pctile prob2, p(2.5,97.5) ;
    scalar `lo2'=r(r1) ;
    scalar `hi2'=r(r2) ;

    _pctile prob3, p(2.5,97.5) ;
    scalar `lo3'=r(r1) ;
    scalar `hi3'=r(r2) ;

    _pctile prob4, p(2.5,97.5) ;
    scalar `lo4'=r(r1) ;
    scalar `hi4'=r(r2) ;

    _pctile prob5, p(2.5,97.5) ;
    scalar `lo5'=r(r1) ;
    scalar `hi5'=r(r2) ;

        
    _pctile diff0, p(2.5,97.5) ;
    scalar `diff_lo0'= r(r1) ;
    scalar `diff_hi0'=r(r2) ;

    _pctile diff1, p(2.5,97.5) ;
    scalar `diff_lo1'= r(r1) ;
    scalar `diff_hi1'=r(r2) ;

    _pctile diff2, p(2.5,97.5) ;
    scalar `diff_lo2'= r(r1) ;
    scalar `diff_hi2'=r(r2) ;
        
    scalar `prob_hat0'=probhat0 ;
    scalar `prob_hat1'=probhat1 ;
    scalar `prob_hat2'=probhat2 ;
    scalar `prob_hat3'=probhat3 ;
    scalar `prob_hat4'=probhat4 ;
    scalar `prob_hat5'=probhat5 ;

      
    scalar `diff_hat0'=diffhat0 ;
    scalar `diff_hat1'=diffhat1 ;
    scalar `diff_hat2'=diffhat2 ;


    _pctile prob01, p(2.5,97.5) ;
    scalar `lo01'=r(r1) ;
    scalar `hi01'=r(r2) ;

    _pctile prob11, p(2.5,97.5) ;
    scalar `lo11'=r(r1) ;
    scalar `hi11'=r(r2) ;

    _pctile prob21, p(2.5,97.5) ;
    scalar `lo21'=r(r1) ;
    scalar `hi21'=r(r2) ;

    _pctile prob31, p(2.5,97.5) ;
    scalar `lo31'=r(r1) ;
    scalar `hi31'=r(r2) ;
        
    _pctile prob41, p(2.5,97.5) ;
    scalar `lo41'=r(r1) ;
    scalar `hi41'=r(r2) ;

    _pctile prob51, p(2.5,97.5) ;
    scalar `lo51'=r(r1) ;
    scalar `hi51'=r(r2) ;


        
    _pctile diff01, p(2.5,97.5) ;
    scalar `diff_lo01'= r(r1) ;
    scalar `diff_hi01'=r(r2) ;

    _pctile diff11, p(2.5,97.5) ;
    scalar `diff_lo11'= r(r1) ;
    scalar `diff_hi11'=r(r2) ;

    _pctile diff21, p(2.5,97.5) ;
    scalar `diff_lo21'= r(r1) ;
    scalar `diff_hi21'=r(r2) ;

    scalar `prob_hat01'=probhat01 ;
    scalar `prob_hat11'=probhat11 ;
    scalar `prob_hat21'=probhat21 ;
    scalar `prob_hat31'=probhat31 ;
    scalar `prob_hat41'=probhat41 ;
    scalar `prob_hat51'=probhat51 ;




    scalar `diff_hat01'=diffhat01 ;
    scalar `diff_hat11'=diffhat11 ;
    scalar `diff_hat21'=diffhat21 ;


    _pctile delta, p(2.5,97.5) ;
    scalar `delta_lo'= r(r1) ;
    scalar `delta_hi'=r(r2) ;

    _pctile delta1, p(2.5,97.5) ;
    scalar `delta_lo1'= r(r1) ;
    scalar `delta_hi1'=r(r2) ;


    _pctile delta2, p(2.5,97.5) ;
    scalar `delta_lo2'= r(r1) ;
    scalar `delta_hi2'=r(r2) ;

        
    scalar `delta_hat'=deltahat;
    scalar `delta_hat1'=deltahat1;
    scalar `delta_hat2'=deltahat2;


 post mypost (`prob_hat0') (`lo0') (`hi0') (`prob_hat1') (`lo1') (`hi1') (`prob_hat2') (`lo2') (`hi2')
        (`prob_hat3') (`lo3') (`hi3') (`prob_hat4') (`lo4') (`hi4') (`prob_hat5') (`lo5') (`hi5')
        (`diff_hat0') (`diff_lo0') (`diff_hi0') (`diff_hat1') (`diff_lo1') (`diff_hi1') (`diff_hat2') (`diff_lo2') (`diff_hi2')
        (`prob_hat01') (`lo01') (`hi01') (`prob_hat11') (`lo11') (`hi11') (`prob_hat21') (`lo21') (`hi21')
        (`prob_hat31') (`lo31') (`hi31') (`prob_hat41') (`lo41') (`hi41') (`prob_hat51') (`lo51') (`hi51')
        (`diff_hat01') (`diff_lo01') (`diff_hi01') (`diff_hat11') (`diff_lo11') (`diff_hi11') (`diff_hat21') (`diff_lo21') (`diff_hi21')
                (`delta_hat') (`delta_lo') (`delta_hi') (`delta_hat1') (`delta_lo1') (`delta_hi1') (`delta_hat2') (`delta_lo2') (`delta_hi2')

    ;

    } ;

drop    x_betahat0 x_betahat1
        prob0 prob1 prob2 prob3 prob4 prob5
        diff0 diff1 diff2
        probhat0 probhat1 probhat2 probhat3 probhat4 probhat5
        diffhat0 diffhat1 diffhat2
        x_betahat01 x_betahat11
        prob01 prob11 prob21 prob31 prob41 prob51
        diff01 diff11 diff21
        probhat01 probhat11 probhat21 probhat31 probhat41 probhat51
        diffhat01 diffhat11 diffhat21
        delta deltahat delta1 deltahat1 delta2 deltahat2;

    local a=`a'+ 1 ;

    display "." _c ;
    } ;
display "" ; postclose mypost ;



use PredictedValues, clear;

gen ruler = _n;
sum;

saveold PredictedValues, replace;

log close;


